package com.qianniu.napi.admin.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.qianniu.napi.admin.entity.Jinfo;
import com.qianniu.napi.admin.entity.Templatemsg;
import com.qianniu.napi.admin.service.ITemplatemsgService;
import com.qianniu.napi.common.util.ObjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;

/**
 *
 * <p>
 * 
 * </p>
 *
 * @author chenxuebin
 * @since 2019-03-11
 */
@Controller
public class TemplatemsgController {

    private static final Logger logger = LoggerFactory.getLogger(TemplatemsgController.class);

    @Autowired
    private ITemplatemsgService templatemsgService;

    @RequestMapping("/api/templatemsg/list")
    @ResponseBody
    public Jinfo list(@RequestParam(value="status",required = false)String status,
                      @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                      @RequestParam(value = "pageSize", required = false) Integer pageSize,
                      HttpServletRequest req) {
        Jinfo info = new Jinfo();
        if(pageSize==null)pageSize = 10;
        Page page=new Page(pageIndex,pageSize);
        page = templatemsgService.selectPage(page, new EntityWrapper<Templatemsg>()
                .eq("uid", ObjectUtil.getUid())
                .eq("state", 1));
        info.setInfo(page.getRecords());
        return info;
    }

    @RequestMapping("/api/templatemsg/add")
    @ResponseBody
    public Jinfo templatemsgAdd(@Valid Templatemsg templatemsg,
                                BindingResult bindingResult, HttpServletRequest req) {
        Jinfo info = new Jinfo();
        // 字段校验
        if(bindingResult.hasErrors())
        {
            logger.debug( "There are errors! {}", bindingResult );
            info.setMsg("There are errors! "+bindingResult);
            info.setCode(400);
            return info;
        }
        templatemsg.setCreatetime(new Date());
        templatemsg.setMid(ObjectUtil.getMid());
        templatemsg.setMpid(ObjectUtil.getMpid());
        templatemsg.setUid(ObjectUtil.getUid());
        templatemsg.insert();
        return info;
    }

    @RequestMapping("/api/templatemsg/modify")
    @ResponseBody
    public Jinfo templatemsgUpdate(HttpServletRequest req,
                                   Templatemsg templatemsg) {
        Jinfo info = new Jinfo();
        templatemsg.setModifytime(new Date());
        templatemsg.updateById();
        return info;
    }

    @RequestMapping("/api/templatemsg/detail")
    @ResponseBody
    public Jinfo getTemplatemsgDetailById(@RequestParam("token") String token,
                                          @RequestParam("id") long id, HttpServletRequest req
    ) {
        Jinfo info = new Jinfo();
        Templatemsg templatemsg = templatemsgService.selectById(id);
        info.setInfo(templatemsg);
        return info;
    }
}
